<template>
<div style="height:600px;box-sizing:border-box;padding:0 20px;overflow-y: auto; ">
   <el-button class="filter-item" size="mini" type="success" icon="el-icon-back"  @click="back">返回</el-button>	
  <div class="head_title">新增模板</div>
  <el-form ref="form" :model="form"  style="margin-top: 16px;" size="small" label-width="180px">
	<el-form-item label="客户类型"  style="display:inline-block;width:40%;">
			<el-select v-model="form.custKind"  placeholder="请先选择客户类型" style="width: 370px;" clearable>
				<el-option
						v-for="item in dictMap.cust_type"
						:key="item.id"
						:label="item.label"
						:value="item.value"/>
			</el-select>
	</el-form-item>
	<el-form-item label="授信品种"  style="display:inline-block;width:40%;">
			<el-select v-model="form.busiType"  placeholder="请先选择授信品种" style="width: 370px;" clearable>
				<el-option
						v-for="item in dictMap.busi_type"
						:key="item.id"
						:label="item.label"
						:value="item.value"/>
			</el-select>
	</el-form-item>
	<el-form-item label="模板编号"   style="display:inline-block;width:23%;" :required='must'>
         <el-input v-model="reqForm.templateNo" style="width: 370px;"/>
    </el-form-item>
	 <el-form-item style="display:inline-block;width:40%;">
		  <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="searchAndSelect">查询模板并选择</el-button>
	</el-form-item>
	<el-form-item  style="display:inline-block;width:48%;">
		    <el-button class="filter-item" size="mini" type="success"   @click="addTem">确定</el-button>
			<el-button class="filter-item" size="mini" type="success"   @click="back">取消</el-button>
	</el-form-item>	
  </el-form>

  <el-dialog :visible.sync="dialog" title="请选择某个模板" append-to-body width="870px">
	     <el-table v-loading="loading" :data="taskData" size="small" style="width: 100%;" @selection-change="select">
			       <el-table-column type="selection" width="50"></el-table-column>
			       <el-table-column prop="templateNo" label="模板编号"/>
			       <el-table-column prop="reportType" label="报告类型"/>
                   <el-table-column :show-overflow-tooltip="true" prop="createTime" label="创建日期">
						<template slot-scope="scope">
						<span>{{ parseTime(scope.row.createTime) }}</span>
						</template>
					</el-table-column>
                    <el-table-column :show-overflow-tooltip="true" prop="createUser" label="发布状态">
						<template slot-scope="scope">
						<span v-if="scope.row.publishStatus == false">待发布</span>
						<span v-else>已发布</span>
						</template>
					</el-table-column>
		  </el-table>
           <el-pagination
          :total="taskToal"
          :current-page="page + 1"
          style="margin-top: 8px;"
          layout="total, prev, pager, next, sizes"
          @size-change="sizeChangeTask"
          @current-change="pageChangeTask"/>

  </el-dialog>


  </div>
</template>

<script>
import { get, update } from '@/api/alipay'
import { general,list,credentials,addCredentials,effective} from '@/api/custComBaseinfo'
import {configList,templaList,addTemplate} from '@/api/loanManageInfo'
import { contractDetails } from '@/api/loanManageInfo'
import initData from '@/mixins/initData'
import initDict from '@/mixins/initDict'
import { parseTime } from '@/utils/index'
import checkPermission from '@/utils/permission'
export default {
  name: 'Config',
  mixins: [initData,initDict],
  data() {
    return {
	  dialog:false,
	  loading: false,
	  addLoading:false,
	  id:'',
      form: { 
		    page: 0, 
	    	size: 10,
		    sort:'id,desc',
		   custKind:'',
		   busiType:'',
	   },
	   
	   formm:{
		    page: 0, 
	    	size: 10,
		    sort:'id,desc',
	   },


	   reqForm:{
		   templateNo:'' ,
		   templateId:''
	   },

       taskToal:null,
	   taskData:[],
	   ids:[],
	   id:'',

       templateIds:[],
	   templateid:'',



	   must:true,
		reqLoading:true,
		reqId:'',
		reqIds:[],
	    addForm:{
		   cardCountry:'',
		   cardType:'',
		   cardNum:'',
		   cardEndDate:'',
		   cardStatus:'',
		   cardPublishCity:'',
		   isMainCard:''
		},
		addRules: {
				cardCountry: [
				  { required: true, message: '请输入证件国别', trigger: 'blur' }
				],
				cardType: [
				  { required: true, message: '请输入证件类型', trigger: 'blur' }
				],
				cardNum: [
				  { required: true, message: '请输入证件编号', trigger: 'blur' }
				],
				cardEndDate: [
				  { required: true, message: '请输入证件到期日期', trigger: 'blur' }
				],
				cardStatus: [
				  { required: true, message: '请输入证件状态', trigger: 'blur' }
				],
				cardPublishCity: [
				  { required: true, message: '请输入证件发布城市', trigger: 'blur' }
				],
		        isMainCard: [
				  { required: true, message: '请输入是否主证件', trigger: 'blur' }
				]

		},

		effectiveParams:{
		   id:'',
		   cardStatus:''
		},
      rules: {
        appID: [
          { required: true, message: '请输入appID', trigger: 'blur' }
        ],
        sysServiceProviderId: [
          { required: true, message: '请输入商家账号', trigger: 'blur' }
        ],
        privateKey: [
          { required: true, message: '商户私钥不能为空', trigger: 'blur' }
        ],
        publicKey: [
          { required: true, message: '支付宝公钥不能为空', trigger: 'blur' }
        ],
        returnUrl: [
          { required: true, message: '回调地址不能为空', trigger: 'blur' }
        ],
        notifyUrl: [
          { required: true, message: '回调地址不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
	  this.$nextTick(() => {
		  this.getDictMap('cust_type,busi_type')
		  console.log('dictMap==================',this.dictMap)
	  })
  },
  methods: {
	   parseTime,
	   checkPermission,
	back(){
		 this.$router.go(-1)
	},
	handleSelectionChange(val) {
		  console.log('val',val)
		   val.forEach((v) => {
               this.reqIds.push(v.id)
		   });
		   console.log('ids',this.reqIds)
		   this.reqId = this.reqIds.toString()
		   this.reqIds = []
	},
    searchAndSelect(){
		this.dialog = true;

		configList(this.formm).then(res => {
           // this.total = res.totalElementst
			//this.data = res.content
			this.taskData = res.content
			this.taskToal = res.totalElements
			setTimeout(() => {
                this.loading = false
             }, 170)
        }).catch(err => {
            this.loading = false
            console.log(err.response.data.message)
        })
	},
    select(val){
         console.log('val',val)
		   val.forEach((v) => {
			   this.ids.push(v.templateNo)
			   this.templateIds.push(v.id)
		   });
		   console.log('ids',this.ids)
		   this.id = this.ids.toString()
		   this.templateId = this.templateIds.toString()
		   this.ids = []
		   this.templateIds = []
		   console.log('this.id',this.id) 
		   console.log('this.id-------',this.id.split(',').length) 
		   if(this.id!=''&&this.id.split(',').length>=2){
				 this.$message.warning('只能选择某一项')
				 return
		   }
		   this.reqForm.templateNo = this.id;
           this.reqForm.templateId = this.templateId;
		   console.log('this.reqForm.templateId',this.reqForm.templateNo)
		
	},
	addTem(){
	   const params = {
		   templateNo:this.reqForm.templateNo,
		   custKind:this.form.custKind,
		   busiType:this.form.busiType,
		   templateId:this.reqForm.templateId,
	   }
	   if(params.templateNo == ''){
		   this.$message.warning('请选择模板编号,模板编号不能为空！')
		   return;
	   }
		addTemplate(params).then(res => {
					//this.resetForm()
					this.$notify({
					title: '新增成功',
					type: 'success',
					duration: 2500
					})
					this.loading = false
					this.$router.go(-1)
				}).catch(err => {
					this.loading = false
					console.log(err.response.data.message)
		})



	},
	pageChangeTask(current){
		console.log('current-1',current-1)
		this.formm.page = current-1;

		configList(this.formm).then(res => {
          //  this.total = res.totalElements
		//	this.data = res.content
			this.taskData = res.content
			this.taskToal = res.totalElements
			setTimeout(() => {
                this.loading = false
             }, 170)
        }).catch(err => {
            this.loading = false
            console.log(err.response.data.message)
        })

	},
	sizeChangeTask(size){
		console.log('size',size)
		this.formm.size = size;

		configList(this.formm).then(res => {
           // this.total = res.totalElements
			//this.data = res.content
			this.taskData = res.content
			this.taskToal = res.totalElements
			setTimeout(() => {
                this.loading = false
             }, 170)
        }).catch(err => {
            this.loading = false
            console.log(err.response.data.message)
        })
	},




	add(){
		this.dialog = true;
	},
	cancelAdd(){
		this.resetForm()
	},
	resetForm() {
      this.dialog = false
	  this.$refs['forms'].resetFields()
	  this.addForm ={
           custId:'',

		   cardCountry:'',
		   cardType:'',
		   cardNum:'',
		   cardEndDate:'',
		   cardStatus:'',
		   cardPublishCity:'',
		   isMainCard:''
	  }
	},
	
 

  }
}
</script>

<style scoped> 
  .head_title{
    height:40px;line-height:40px;border-bottom: 2px solid #ce2731;color:#ce2731
  }
  .space{
	  margin-top:30px;
	  margin-bottom:10px
  }
   .one{
	   display:inline-block;
	   width:86.5%;
   }
   .two{
	   display:inline-block;
	   width:50%;
	
   }


</style>
